package com.hourse.app.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

/**
 * @使用描述: ImageUtil
 */
@Slf4j
public class ImageUtil {

    public static void DrawImage(String backImagePath,
                                 java.util.List<DrawParam> drawParamList,
                                 String outPath, String outName) throws IOException {

        //System.out.println("url  ======> " + backImagePath);
        log.info("url  ======> " + backImagePath);

        BufferedImage backImg = ImageIO.read(new File(backImagePath));
        Graphics2D graphics = backImg.createGraphics();
        graphics.setColor(Color.BLACK);

        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        for (DrawParam drawParam : drawParamList) {
            graphics.setFont(new Font("宋体", Font.BOLD, 26));
            graphics.drawString(drawParam.text, drawParam.x, drawParam.y);
        }
        graphics.dispose();

        File file = new File(outPath);

        if (!file.exists()) {
            file.mkdirs();
        }

        ImageIO.write(backImg, "jpeg", new File(outPath + outName));
    }


    public static byte[] imgArray(String filePath) throws IOException {

        InputStream in = new FileInputStream(filePath);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024 * 4];
        int n = 0;
        while ((n = in.read(buffer)) != -1) {
            out.write(buffer, 0, n);
        }
        return out.toByteArray();

    }

    public static ArrayList<byte[]> imgArrays(JSONObject filePaths) throws IOException {

        ArrayList<byte[]> list = new ArrayList<byte[]>();
        for (int i = 0; i < filePaths.size(); i++) {
            InputStream in = new FileInputStream((String) filePaths.get(i));
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024 * 4];
            int n = 0;
            while ((n = in.read(buffer)) != -1) {
                out.write(buffer, 0, n);
            }

            // return out.toByteArray();
            list.add(out.toByteArray());

        }

        return list;

    }

    public boolean downloadFromHttp(String urlPath, String fileNamePath) {
        try {
            URL url = new URL(urlPath);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            DataInputStream in = new DataInputStream(connection.getInputStream());


            DataOutputStream out = new DataOutputStream(new FileOutputStream(fileNamePath));
            byte[] buffer = new byte[4096];
            int count = 0;
            while ((count = in.read(buffer)) > 0) {
                out.write(buffer, 0, count);
            }
            out.close();
            in.close();
            return true;
        } catch (Exception e) {
            return false;
        }

    }


    /**
     * 转换BufferedImage 数据为byte数组
     *
     * @param image
     * Image对象
     * @param format
     * image格式字符串.如"jpg","png"
     * @return byte数组
     */
    public static byte[] imageToBytes(BufferedImage image, String format) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, format, out);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return out.toByteArray();
    }

    public static void main(String[] args) throws IOException {

        // java.util.List<DrawParam> drawParamList = new ArrayList();
        // drawParamList.add(new DrawParam(240, 200, 17, "北京华道征信有限公司"));
        // drawParamList.add(new DrawParam(940, 200, 17, null));
        // String outPath = "/C:/Users/Administrator/Desktop/financebill/open-financ-bill-2.jpg";
        // String backImage = "/C:/Users/Administrator/Desktop/financebill/open-financ-bill.jpg";
        // ImageUtil.DrawImage(backImage, drawParamList, outPath);


        java.util.List<DrawParam> drawParamList = new ArrayList();
        drawParamList.add(new DrawParam(250, 180, 17, "国誉创富国际金融服务"));
        drawParamList.add(new DrawParam(250, 220, 17, "专项债供应链综合服务管理系统"));


        String outPath = "/C:/Users/Administrator/Desktop/financebill/open-financ-bill-2.jpg";
        String backImage = "/C:/Users/Administrator/Desktop/financebill/template/open-financ-bill.jpg";
        ImageUtil.DrawImage(backImage,drawParamList, "/C:/Users/Administrator/Desktop/financebill", "open-financ-bill-2.jpg");


        // byte[] image = imgArray("C:\\Users\\Administrator\\Desktop\\financebill\\open-financ-bill-37990304135890696-ok.png");
        // for (byte b : image) {
        //     System.out.println(b);
        //
        // }
        // System.out.println(image);

        // downloadFromHttp();


    }
}
